<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>AutoComplete</title>
    <link rel="stylesheet" href="http://yui.yahooapis.com/3.4.0pr3/build/cssgrids/grids-min.css">
    <link rel="stylesheet" href="../assets/css/main.css">
    <link rel="stylesheet" href="../assets/vendor/prettify/prettify-min.css">
    <script src="../../build/yui/yui-min.js"></script>
</head>
<body>

<div id="doc">
    <h1>AutoComplete</h1>

    
        <a href="#toc" class="jump">Jump to Table of Contents</a>
    

    <div class="yui3-g">
        <div id="main" class="yui3-u">
            <div class="content"><div class="intro">
<p>
<img src="../assets/autocomplete/img/autocomplete-01.png" alt="Screenshot of the AutoComplete list widget" style="border: 1px solid #bfbfbf; float:right; height:161px; margin: 0 0 8px 8px; width:272px;">
The AutoComplete widget provides a flexible, configurable, and accessible implementation of the <a href="http://developer.yahoo.com/ypatterns/selection/autocomplete.html">AutoComplete design pattern</a>, which offers suggestions or provides some other form of filtering or completion as a user types text in an input field.
</p>

<p>
In addition to the core logic for filtering and completion, AutoComplete also provides options for custom filtering, highlighting, and formatting of results; delimited queries; result retrieval from a variety of local and remote sources including <a href="http://developer.yahoo.com/yql/">YQL</a>, JSONP, and XHR; and more.
</p>

<p>
AutoComplete is also built to be modular and easy to extend so that it can be used as the basis for custom implementations and widgets.
</p>
</div>

<h2 id="getting-started">Getting Started</h2>

<p>
To include the source files for AutoComplete and its dependencies, first load
the YUI seed file if you haven't already loaded it.
</p>

<pre class="code prettyprint">&lt;script src=&quot;http:&#x2F;&#x2F;yui.yahooapis.com&#x2F;3.4.1&#x2F;build&#x2F;yui&#x2F;yui-min.js&quot;&gt;&lt;&#x2F;script&gt;</pre>


<p>
Next, create a new YUI instance for your application and populate it with the
modules you need by specifying them as arguments to the <code>YUI().use()</code> method.
YUI will automatically load any dependencies required by the modules you
specify.
</p>

<pre class="code prettyprint">&#x2F;&#x2F; Create a new YUI instance and populate it with the required modules.
YUI().use(&#x27;autocomplete&#x27;, function (Y) {
    &#x2F;&#x2F; AutoComplete is available and ready for use. Add implementation
    &#x2F;&#x2F; code here.
});</pre>


<p>
For more information on creating YUI instances and on the
<a href="http://yuilibrary.com/yui/docs/api/classes/YUI.html#method_use"><code>use()</code> method</a>, see the
documentation for the <a href="../yui/index.html">YUI Global object</a>.
</p>


<h2 id="using-autocomplete">Using AutoComplete</h2>
<h3 id="quick-start">Quick Start</h3>

<p>
In a hurry? Here's how to get up and running with AutoComplete in just a few lines of code. The following examples demonstrate how to use AutoComplete with several common result sources. Pick the one that most closely matches your needs (you only need one!).
</p>

<pre class="code prettyprint">YUI().use(&#x27;autocomplete&#x27;, &#x27;autocomplete-highlighters&#x27;, function (Y) {

  &#x2F;&#x2F; Add the yui3-skin-sam class to the body so the default
  &#x2F;&#x2F; AutoComplete widget skin will be applied.
  Y.one(&#x27;body&#x27;).addClass(&#x27;yui3-skin-sam&#x27;);

  &#x2F;&#x2F; The following examples demonstrate some of the different
  &#x2F;&#x2F; result sources AutoComplete supports. You only need to
  &#x2F;&#x2F; pick one, you don&#x27;t need them all. Assume the &#x27;#ac-input&#x27;
  &#x2F;&#x2F; element id used in this example refers to an &lt;input&gt;
  &#x2F;&#x2F; element on the page.

  &#x2F;&#x2F; Array source. Replace the example array with any array.
  Y.one(&#x27;#ac-input&#x27;).plug(Y.Plugin.AutoComplete, {
    resultHighlighter: &#x27;phraseMatch&#x27;,
    source: [&#x27;foo&#x27;, &#x27;bar&#x27;, &#x27;baz&#x27;]
  });

  &#x2F;&#x2F; YQL source. Leave the {query} placeholder as is; AutoComplete
  &#x2F;&#x2F; will replace it automatically.
  Y.one(&#x27;#ac-input&#x27;).plug(Y.Plugin.AutoComplete, {
    resultHighlighter: &#x27;phraseMatch&#x27;,
    source: &#x27;select * from search.suggest where query=&quot;{query}&quot;&#x27;
  });

  &#x2F;&#x2F; JSONP URL source. Leave the {query} and {callback} placeholders
  &#x2F;&#x2F; as is; AutoComplete will replace them automatically.
  Y.one(&#x27;#ac-input&#x27;).plug(Y.Plugin.AutoComplete, {
    resultHighlighter: &#x27;phraseMatch&#x27;,
    source: &#x27;http:&#x2F;&#x2F;example.com&#x2F;search.jsonp?q={query}&amp;callback={callback}&#x27;
  });

  &#x2F;&#x2F; XHR URL source (no callback). Leave the {query} placeholder
  &#x2F;&#x2F; as is; AutoComplete will replace it automatically.
  Y.one(&#x27;#ac-input&#x27;).plug(Y.Plugin.AutoComplete, {
    resultHighlighter: &#x27;phraseMatch&#x27;,
    source: &#x27;http:&#x2F;&#x2F;example.com&#x2F;search.json?q={query}&#x27;
  });

});</pre>


<p>
In most cases, one of these examples should be enough to get you started. For a more complete discussion of how to use, configure, and customize AutoComplete, read on.
</p>

<h3 id="instantiating-autocomplete">Instantiating AutoComplete</h3>

<p>
There are two ways to instantiate an AutoComplete widget: you can plug <code>Y.Plugin.AutoComplete</code> into an existing <code>Y.Node</code> instance, or you can create a new standalone instance of the <code>Y.AutoComplete</code> class.
</p>

<p>
Both instantiation methods provide the same AutoComplete functionality, so feel free to use whichever one you prefer. Throughout this guide and in the examples, the plugin method is used most, but the class method will work equally well in all cases.
</p>

<p>
Whichever instantiation method you choose, be sure to add the <code>yui3-skin-sam</code> classname to the page's <code>&lt;body&gt;</code> element or to a parent element of the AutoComplete widget in order to apply the default CSS skin:
</p>

<pre class="code prettyprint">&lt;body class=&quot;yui3-skin-sam&quot;&gt;</pre>


<p>
See the <a href="#skinning">Skinning</a> section below for more info.
</p>

<h4 id="as-a-plugin">As a Plugin</h4>

<p>
To instantiate AutoComplete as a plugin, use the <code>plug()</code> method to attach it to an existing <code>Y.Node</code> instance. The node must be an <code>&lt;input&gt;</code> or <code>&lt;textarea&gt;</code> element. You may also provide a configuration object containing AutoComplete config attributes, but this isn't required.
</p>

<pre class="code prettyprint">Y.one(&#x27;#ac-input&#x27;).plug(Y.Plugin.AutoComplete);</pre>


<p>
In most cases, you'll at least want to specify a <code>source</code> attribute, which tells AutoComplete where to get results. The simplest type of source is an array of strings.
</p>

<pre class="code prettyprint">Y.one(&#x27;#ac-input&#x27;).plug(Y.Plugin.AutoComplete, {
  source: [&#x27;friends&#x27;, &#x27;Romans&#x27;, &#x27;countrymen&#x27;]
});</pre>


<p>
Once you've plugged AutoComplete into a node, you can access the AutoComplete instance through the node's <code>ac</code> property.
</p>

<pre class="code prettyprint">var inputNode = Y.one(&#x27;#ac-input&#x27;);

inputNode.plug(Y.Plugin.AutoComplete);
inputNode.ac.set(&#x27;source&#x27;, [&#x27;friends&#x27;, &#x27;Romans&#x27;, &#x27;countrymen&#x27;]);</pre>


<p>
When using AutoComplete as a plugin, the AutoComplete widget markup will be rendered automatically as soon as it's plugged into a node instance. If you'd like to defer rendering until a time of your choosing, set the <code>render</code> config attribute to <code>false</code>.
</p>

<pre class="code prettyprint">&#x2F;&#x2F; Don&#x27;t render immediately.
Y.one(&#x27;#ac-input&#x27;).plug(Y.Plugin.AutoComplete, {render: false});

&#x2F;&#x2F; Render only when I say so.
Y.one(&#x27;#ac-input&#x27;).ac.render();</pre>


<p>
By default, the AutoComplete widget markup is appended to the parent node of the node it's plugged into. If you would rather render the markup inside a different parent, pass a CSS selector or <code>Y.Node</code> instance to the <code>render</code> config attribute or the <code>render()</code> method.
</p>

<pre class="code prettyprint">&#x2F;&#x2F; Render inside the #ac-parent node.
Y.one(&#x27;#ac-input&#x27;).plug(Y.Plugin.AutoComplete, {render: &#x27;#ac-parent&#x27;});</pre>


<p>
For more on available configuration attributes, see the <a href="#configuring-autocomplete">Configuring AutoComplete</a> section below. For more on the different result sources AutoComplete supports, see <a href="#result-sources">Result Sources</a>.
</p>

<h4 id="as-a-class">As a Class</h4>

<p>
To instantiate AutoComplete as a class, create a new instance of <code>Y.AutoComplete</code> and specify an <code>inputNode</code> config value. The <code>inputNode</code> must be a CSS selector, <code>Y.Node</code> instance, or DOM element referencing an <code>&lt;input&gt;</code> or <code>&lt;textarea&gt;</code> element that already exists on the page.
</p>

<pre class="code prettyprint">var ac = new Y.AutoComplete({inputNode: &#x27;#ac-input&#x27;});</pre>


<p>
<code>inputNode</code> is the only required configuration attribute, but in most cases you'll also want to specify a <code>source</code> attribute, which tells AutoComplete where to get results. The simplest type of source is an array of strings.
</p>

<pre class="code prettyprint">var ac = new Y.AutoComplete({
  inputNode: &#x27;#ac-input&#x27;,
  source   : [&#x27;friends&#x27;, &#x27;Romans&#x27;, &#x27;countrymen&#x27;]
});</pre>


<p>
When instantiated as a class, the AutoComplete widget markup is not rendered automatically. To render it, either set the <code>render</code> config attribute to <code>true</code> at instantiation time or call the AutoComplete instance's <code>render()</code> method later.
</p>

<pre class="code prettyprint">&#x2F;&#x2F; Render immediately.
var ac = new Y.AutoComplete({
  inputNode: &#x27;#ac-input&#x27;,
  render   : true
});

&#x2F;&#x2F; Don&#x27;t render immediately.
var ac = new Y.AutoComplete({inputNode: &#x27;#ac-input&#x27;});

&#x2F;&#x2F; Render only when I say so.
ac.render();</pre>


<p>
By default, the AutoComplete widget markup is appended to the parent node of the <code>inputNode</code> when the widget is rendered. If you would rather render the markup inside a different parent, pass a CSS selector or <code>Y.Node</code> instance to the <code>render</code> config attribute or the <code>render()</code> method.
</p>

<pre class="code prettyprint">&#x2F;&#x2F; Render inside the #ac-parent node.
var ac = new Y.AutoComplete({inputNode: &#x27;#ac-input&#x27;});
ac.render(&#x27;#ac-parent&#x27;);</pre>


<p>
For more on available configuration attributes, see the <a href="#configuring-autocomplete">Configuring AutoComplete</a> section below. For more on the different result sources AutoComplete supports, see <a href="#result-sources">Result sources</a>.
</p>

<h3 id="dom-structure">DOM Structure</h3>

<p>
When the AutoComplete widget is rendered, it will add the CSS class <code>yui3-aclist-input</code> to the specified <code>inputNode</code>, along with several ARIA attributes.
</p>

<pre class="code prettyprint">&lt;!-- Before AutoComplete is rendered --&gt;
&lt;input id=&quot;ac-input&quot; type=&quot;text&quot;&gt;

&lt;!-- After AutoComplete is rendered --&gt;
&lt;input id=&quot;ac-input&quot; type=&quot;text&quot; class=&quot;yui3-aclist-input&quot;
    aria-autocomplete=&quot;list&quot; aria-expanded=&quot;false&quot;
    aria-owns=&quot;yui_3_3_0_1_129140941365181&quot; autocomplete=&quot;off&quot;&gt;</pre>


<p>
AutoComplete will also add markup for the list widget. By default, the list markup will be appended to the parent node that contains the <code>inputNode</code>.
</p>

<pre class="code prettyprint">&lt;!-- AutoCompleteList widget markup with sample results --&gt;
&lt;div id=&quot;yui_3_3_0_1_129140941365147&quot;
    class=&quot;yui3-widget yui3-aclist yui3-widget-positioned yui3-aclist-hidden&quot;
    style=&quot;position: absolute; width: 254px; left: 13px; top: 32px;&quot;
    aria-hidden=&quot;true&quot;&gt;

  &lt;div id=&quot;yui_3_3_0_1_129140941365150&quot; class=&quot;yui3-aclist-content&quot;&gt;
    &lt;ul class=&quot;yui3-aclist-list&quot; id=&quot;yui_3_3_0_1_129140941365181&quot; role=&quot;listbox&quot;&gt;
      &lt;li class=&quot;yui3-aclist-item&quot; id=&quot;yui_3_3_0_1_1291409413651452&quot; role=&quot;option&quot;&gt;friends&lt;&#x2F;li&gt;
      &lt;li class=&quot;yui3-aclist-item&quot; id=&quot;yui_3_3_0_1_1291409413651454&quot; role=&quot;option&quot;&gt;Romans&lt;&#x2F;li&gt;
      &lt;li class=&quot;yui3-aclist-item&quot; id=&quot;yui_3_3_0_1_1291409413651456&quot; role=&quot;option&quot;&gt;countrymen&lt;&#x2F;li&gt;
    &lt;&#x2F;ul&gt;
  &lt;&#x2F;div&gt;

&lt;&#x2F;div&gt;</pre>


<p>
For information on skinning the AutoComplete widget, see the <a href="#skinning">Skinning</a> section below.
</p>

<h3 id="configuring-autocomplete">Configuring AutoComplete</h3>

<p>
Except for <code>inputNode</code>, all configuration attributes are optional. These lists only contain the most interesting attributes. For a complete list of all attributes, please refer to the <a href="http://yuilibrary.com/yui/docs/api/modules/autocomplete.html">API docs</a>.
</p>

<h4 id="base-config-attributes">Base Config Attributes</h4>

<p>
These attributes are provided by <code>AutoCompleteBase</code>, which is the core foundation for the AutoComplete widget. They are available on all AutoComplete instances.
</p>

<table>
  <thead>
    <tr>
      <th>Attribute</th>
      <th>Default</th>
      <th>Description</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><code>allowBrowserAutocomplete</code></td>
      <td><code>false</code></td>
      <td>
        Whether or not to enable the browser's built-in autocomplete functionality for input fields.
      </td>
    </tr>

    <tr>
      <td><code>inputNode</code></td>
      <td><em>none</em></td>
      <td>
        <strong>Required</strong>. <code>&lt;input&gt;</code> or <code>&lt;textarea&gt;</code> node to monitor for changes.
      </td>
    </tr>

    <tr>
      <td><code>maxResults</code></td>
      <td>0</td>
      <td>
        Maximum number of results to display. A value of <code>0</code> or less will allow an unlimited number of results.
      </td>
    </tr>

    <tr>
      <td><code>minQueryLength</code></td>
      <td>1</td>
      <td>
        Minimum number of characters that must be entered before a <code>query</code> event will be fired. A value of <code>0</code> allows empty queries; a negative value will effectively disable all <code>query</code> events and turn AutoComplete off.
      </td>
    </tr>

    <tr>
      <td><code>queryDelay</code></td>
      <td>100</td>
      <td>
        <p>
        Number of milliseconds to wait after user input before triggering a <code>query</code> event. If new input occurs before this delay is over, the previous input event will be ignored and a new delay will begin.
        </p>

        <p>
        This is useful to throttle queries to a remote data source, and to avoid distracting the user by showing them less relevant results before their typing pauses.
        </p>
      </td>
    </tr>

    <tr>
      <td><code>queryDelimiter</code></td>
      <td><code>null</code></td>
      <td>
        Query delimiter string. When a delimiter is configured, the input value will be split on the delimiter, and only the last portion will be used in autocomplete queries and updated when the <code>query</code> attribute is modified. See <a href="#using-query-delimiters">Using Query Delimiters</a> below for details.
      </td>
    </tr>

    <tr>
      <td><code>requestTemplate</code></td>
      <td><code>null</code></td>
      <td>
        Source request template. This can be a function that accepts a query as a parameter and returns a string, or it can be a string containing the placeholder "{query}", which will be replaced with the URI-encoded query. The resulting string will be appended to the request URL when the <code>source</code> attribute is set to a remote DataSource, JSONP URL, or XHR URL.
      </td>
    </tr>

    <tr>
      <td><code>resultFilters</code></td>
      <td><code>[]</code></td>
      <td>
        Result filter name, function, or array of filter names and/or functions. See <a href="#filtering-results">Filtering Results</a> for details.
      </td>
    </tr>

    <tr>
      <td><code>resultFormatter</code></td>
      <td><code>null</code></td>
      <td>
        Function that should be used to format results. See <a href="#writing-result-formatters">Writing Result Formatters</a> for details.
      </td>
    </tr>

    <tr>
      <td><code>resultHighlighter</code></td>
      <td><code>null</code></td>
      <td>
        Result highlighter name or function. See <a href="#highlighting-results">Highlighting Results</a> for details.
      </td>
    </tr>

    <tr>
      <td><code>resultListLocator</code></td>
      <td><code>null</code></td>
      <td>
        Locator string or function that should be used to extract an array of results from a non-array response. See <a href="#locating-results">Locating Results</a> for details.
      </td>
    </tr>

    <tr>
      <td><code>resultTextLocator</code></td>
      <td><code>null</code></td>
      <td>
        Locator string or function that should be used to extract a plain text string from a non-string result item. See <a href="#locating-results">Locating Results</a> for details.
      </td>
    </tr>

    <tr>
      <td><code>source</code></td>
      <td><code>null</code></td>
      <td>
        Source for autocomplete results. The following source types are supported: Array, DataSource, Function, Object, JSONP URL (string), XHR URL (string), YQL query (string). For details on each source type, see <a href="#result-sources">Result Sources</a>.
      </td>
    </tr>
  </tbody>
</table>

<h4 id="list-config-attributes">List Config Attributes</h4>

<p>
These attributes are provided by <code>AutoCompleteList</code>, which is the implementation for the AutoComplete list widget. They are available on all instances of <code>AutoComplete</code> or <code>AutoCompleteList</code>, as well as on instances of the AutoComplete plugin.
</p>

<table>
  <thead>
    <tr>
      <th>Attribute</th>
      <th>Default</th>
      <th>Description</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><code>activateFirstItem</code></td>
      <td><code>false</code></td>
      <td>
        If <code>true</code>, the first item in the result list will be activated by default when the list is initially displayed and when results change.
      </td>
    </tr>

    <tr>
      <td><code>align</code></td>
      <td>
<pre><code>{
  node: <em>inputNode</em>,
  points: ['tl', 'bl']
}</code></pre>
      </td>
      <td>
        Widget alignment config. The <code>node</code> property is the element with which the result list should be aligned, and the <code>points</code> property specifies (by default) that the top left of the list should be aligned with the bottom left of the <code>inputNode</code>.
      </td>
    </tr>

    <tr>
      <td><code>alwaysShowList</code></td>
      <td><code>false</code></td>
      <td>
        If <code>true</code>, the list will remain visible even when there are no results to display and the <code>inputNode</code> is not focused.
      </td>
    </tr>

    <tr>
      <td><code>circular</code></td>
      <td><code>true</code></td>
      <td>
        If <code>true</code>, keyboard navigation will wrap around to the opposite end of the list when navigating past the first or last item.
      </td>
    </tr>

    <tr>
      <td><code>scrollIntoView</code></td>
      <td><code>false</code></td>
      <td>
        If <code>true</code>, the viewport will be scrolled when necessary to ensure that the active list item is visible.
      </td>
    </tr>

    <tr>
      <td><code>tabSelect</code></td>
      <td><code>true</code></td>
      <td>
        If <code>true</code>, pressing the tab key while the list is visible will select the active item, if any.
      </td>
    </tr>
  </tbody>
</table>

<h4 id="aligning-the-list">Aligning the List</h4>

<p>
By default, the autocomplete dropdown list will be automatically aligned with the bottom left corner of the input node it's attached to, and its width will be set to match the input node's. You can change the alignment of the list by specifying a custom value for the <code>align</code> attribute.
</p>

<p>
For example, to align the top left of the list with the top left of a particular node on the page (such as its container):
</p>

<pre class="code prettyprint">Y.one(&#x27;#ac-input&#x27;).plug(Y.Plugin.AutoComplete, {
  align: {
      node  : &#x27;#container&#x27;,
      points: [&#x27;tl&#x27;, &#x27;tl&#x27;]
  }
});</pre>


<p>
See the <a href="http://yuilibrary.com/yui/docs/api/classes/WidgetPositionAlign.html">WidgetPositionAlign API docs</a> for more details on alignment configs.
</p>

<h4 id="using-query-delimiters">Using Query Delimiters</h4>

<p>
Using the <code>queryDelimiter</code> attribute, you can specify a delimiter string that should be used to split the value of the input field.
</p>

<p>
When a delimiter is set, the <code>query</code> attribute will only reflect the last delimited item in the input value, and only this item will be used for completion (the full input value will still be available from the <code>value</code> attribute).
</p>

<p>
For example, if <code>queryDelimiter</code> is set to <code>&#x27;,&#x27;</code> and the input node's value is <code>&#x27;foo, bar, baz&#x27;</code>, then the value of the <code>query</code> attribute will be <code>&#x27;baz&#x27;</code>.
</p>

<pre class="code prettyprint">var inputNode = Y.one(&#x27;#ac-input&#x27;);

inputNode.set(&#x27;value&#x27;, &#x27;foo, bar, baz&#x27;);

inputNode.plug(Y.Plugin.AutoComplete, {
  queryDelimiter: &#x27;,&#x27;
});

Y.log(inputNode.ac.get(&#x27;query&#x27;)); &#x2F;&#x2F; =&gt; &#x27;baz&#x27;
Y.log(inputNode.get(&#x27;value&#x27;));    &#x2F;&#x2F; =&gt; &#x27;foo, bar, baz&#x27;</pre>


<p>
When the user selects an item from the result list, the selected item will replace only the last delimited item in the input value rather than replacing the entire value, and another delimiter string will be automatically appended to the value so that the user can continue typing and getting suggestions.
</p>

<h3 id="autocomplete-events">AutoComplete Events</h3>

<p>
These lists only contain the most interesting events. For a complete list, please refer to the <a href="http://yuilibrary.com/yui/docs/api/modules/autocomplete.html">API docs</a>.
</p>

<h4 id="base-events">Base Events</h4>

<p>
These events are provided by <code>AutoCompleteBase</code>, which is the core foundation for the AutoComplete widget. They are available on all AutoComplete instances.
</p>

<table>
  <thead>
    <tr>
      <th>Event</th>
      <th>When</th>
      <th>Payload</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><code>clear</code></td>
      <td>
        The query has been completely cleared or no longer meets the minimum query length requirement.
      </td>
      <td>
        <dl>
          <dt><code>prevVal</code> <i>(String)</i></dt>
          <dd>
            Value of the query before it was cleared.
          </dd>
        </dl>
      </td>
    </tr>

    <tr>
      <td><code>query</code></td>
      <td>
        The value of the input field has changed and the new value meets the criteria necessary to generate an autocomplete query. Can be prevented to stop the query from being sent.
      </td>
      <td>
        <dl>
          <dt><code>inputValue</code> <i>(String)</i></dt>
          <dd>
            Full contents of the text input field or textarea that generated the query.
          </dd>

          <dt><code>query</code> <i>(String)</i></dt>
          <dd>
            The query itself. This is the string that will be sent to the result source to request results. It may or may not be the same as <code>inputValue</code>.
          </dd>
        </dl>
      </td>
    </tr>

    <tr>
      <td><code>results</code></td>
      <td>
        Results are received from the result source. If no source has been set, this event will not fire.
      </td>
      <td>
        <dl>
          <dt><code>data</code> <i>(Array|Object)</i></dt>
          <dd>
            Raw, unfiltered result data from the source (if available).
          </dd>

          <dt><code>query</code> <i>(String)</i></dt>
          <dd>
            Query that generated these results.
          </dd>

          <dt><code>results</code> <i>(Array)</i></dt>
          <dd>
            Array of filtered, formatted, and highlighted results. Each item in the array is an object with the following properties:

            <dl>
              <dt><code>display</code> <i>(Node|HTMLElement|String)</i></dt>
              <dd>
                Formatted result HTML suitable for display to the user. If no custom formatter is set, this will be an HTML-escaped version of the string in the <code>text</code> property.
              </dd>

              <dt><code>highlighted</code> <i>(String)</i></dt>
              <dd>
                Highlighted (but not formatted) result text. This property will
                only be set if a highlighter is in use.
              </dd>

              <dt><code>raw</code> <i>(mixed)</i></dt>
              <dd>
                Raw, unformatted result in whatever form it was provided by the
                <code>source</code>.
              </dd>

              <dt><code>text</code> <i>(String)</i></dt>
              <dd>
                Plain text version of the result, suitable for being inserted
                into the value of a text input field or textarea when the result
                is selected by a user. This value is not HTML-escaped and should
                not be inserted into the page using innerHTML.
              </dd>
            </dl>
          </dd>
        </dl>
      </td>
    </tr>
  </tbody>
</table>

<h4 id="list-events">List Events</h4>

<p>
These events are provided by <code>AutoCompleteList</code>, which is the implementation for the AutoComplete list widget. They are available on all instances of <code>AutoComplete</code> or <code>AutoCompleteList</code>, as well as on instances of the AutoComplete plugin.
</p>

<table>
  <thead>
    <tr>
      <th>Event</th>
      <th>When</th>
      <th>Payload</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><code>activeItemChange</code></td>
      <td>
        The active list item (the item currently pre-selected via the keyboard) changes. When the user presses enter, this is the item that will become selected.
      </td>
      <td>
        <dl>
          <dt><code>prevVal</code> <i>(Node|null)</i></dt>
          <dd>
            Node reference for the previously-active list item, or <code>null</code> if there wasn't one.
          </dd>

          <dt><code>newVal</code> <i>(Node|null)</i></dt>
          <dd>
            Node reference for the new active list item, or <code>null</code> if the active item has been cleared.
          </dd>
        </dl>
      </td>
    </tr>

    <tr>
      <td><code>hoveredItemChange</code></td>
      <td>
        The hovered list item (the item currently being hovered over by the mouse) changes.
      </td>
      <td>
        <dl>
          <dt><code>prevVal</code> <i>(Node|null)</i></dt>
          <dd>
            Node reference for the previously hovered list item, or <code>null</code> if there wasn't one.
          </dd>

          <dt><code>newVal</code> <i>(Node|null)</i></dt>
          <dd>
            Node reference for the new hovered list item, or <code>null</code> if there is no hovered item.
          </dd>
        </dl>
      </td>
    </tr>

    <tr>
      <td><code>select</code></td>
      <td>
        A result is selected from the autocomplete list, typically via a keyboard action or a mouse click.
      </td>
      <td>
        <dl>
          <dt><code>itemNode</code> <i>(Node)</i></dt>
          <dd>
            Node reference for the list item that was selected.
          </dd>

          <dt><code>result</code> <i>(Object)</i></dt>
          <dd>
            Result object for the selected result. See the docs for the <code>results</code> event for a description of the result object structure.
          </dd>
        </dl>
      </td>
    </tr>

    <tr>
      <td><code>visibleChange</code></td>
      <td>
        The visibility of the result list changes.
      </td>
      <td>
        <dl>
          <dt><code>prevVal</code> <i>(Boolean)</i></dt>
          <dd>
            Previous visibility of the list.
          </dd>

          <dt><code>newVal</code> <i>(Boolean)</i></dt>
          <dd>
            New visibility of the list.
          </dd>
        </dl>
      </td>
    </tr>
  </tbody>
</table>

<h3 id="result-sources">Result Sources</h3>

<h4 id="arrayobject">Array/Object</h4>

<p>
Set the <code>source</code> attribute to an Array to use that array as the set of results for all queries. Combine this with one or more result filters to filter out results that aren't relevant for the current query.
</p>

<pre class="code prettyprint">Y.one(&#x27;#ac-input&#x27;).plug(Y.Plugin.AutoComplete, {
  source: [&#x27;foo&#x27;, &#x27;bar&#x27;, &#x27;baz&#x27;]
});</pre>


<p>
You can also set <code>source</code> to an Object. When the query matches one of the properties on the object, the value of that property will be used as the results for the query.
</p>

<pre class="code prettyprint">Y.one(&#x27;#ac-input&#x27;).plug(Y.Plugin.AutoComplete, {
  source: {
    a: [&#x27;apple&#x27;, &#x27;airplane&#x27;, &#x27;awesome&#x27;],
    b: [&#x27;banana&#x27;, &#x27;boat&#x27;, &#x27;boring&#x27;],
    c: [&#x27;cherry&#x27;, &#x27;car&#x27;, &#x27;cacophonous&#x27;]
  }
});</pre>


<h4 id="datasource">DataSource</h4>

<p>
Any <a href="../datasource/index.html">YUI DataSource</a> instance may be used as a result source. This is useful if you want to share data between multiple components on a page, or if you need to parse data or apply a DataSchema in a way that isn't feasible with other AutoComplete sources.
</p>

<pre class="code prettyprint">var ds = new Y.DataSource.IO({
  source: &#x27;http:&#x2F;&#x2F;example.com&#x2F;search&#x27;
});

Y.one(&#x27;#ac-input&#x27;).plug(Y.Plugin.AutoComplete, {
  requestTemplate: &#x27;?q={query}&#x27;,
  source: ds
});</pre>


<p>
In this example, the <code>requestTemplate</code> attribute is set to a string containing a <code>{query}</code> placeholder. On each query, the placeholder will be replaced with the URI-encoded query value, and the resulting request string will be appended to the DataSource's source URL, resulting in a final URL like <code>http:&#x2F;&#x2F;example.com&#x2F;search?q=foo</code>.
</p>

<h4 id="function">Function</h4>

<p>
Set the <code>source</code> attribute to a function to use that function as the result source. On each query, the function will be called with two arguments: the current query, and a callback function.
</p>

<p>
If the function is synchronous, meaning that results are available immediately, then it should return an array of results:
</p>

<pre class="code prettyprint">Y.one(&#x27;#ac-input&#x27;).plug(Y.Plugin.AutoComplete, {
  &#x2F;&#x2F; Synchronous source function.
  source: function (query) {
    &#x2F;&#x2F; ...arbitrary logic here...

    return [&#x27;foo&#x27;, &#x27;bar&#x27;, &#x27;baz&#x27;];
  }
});</pre>


<p>
If the function is asynchronous, meaning that it must make a network call or perform some other non-blocking activity from which it can't return results immediately, then an array of results should be passed to the provided callback function when the results become available, and the function should <em>not</em> return a value:
</p>

<pre class="code prettyprint">Y.one(&#x27;#ac-input&#x27;).plug(Y.Plugin.AutoComplete, {
  &#x2F;&#x2F; Asynchronous source function.
  source: function (query, callback) {
    &#x2F;&#x2F; Wait a little while without blocking execution, then provide results.
    &#x2F;&#x2F; This simulates a non-blocking operation such as a JSONP or XHR request.
    setTimeout(function () {
      callback([&#x27;foo&#x27;, &#x27;bar&#x27;, &#x27;baz&#x27;]);
    }, 100);

    &#x2F;&#x2F; Note that the source function doesn&#x27;t return a value.
  }
});</pre>


<h4 id="jsonpxhr-url">JSONP/XHR URL</h4>

<p>
Set the <code>source</code> attribute to a URL string to use that URL as the result source.
</p>

<p>
The URL string must include a <code>{query}</code> placeholder. On each query, AutoComplete will replace this with the current query and will make a request to the URL.
</p>

<p>
If the URL string includes a <code>{callback}</code> placeholder, it will be called using JSONP, and the <code>{callback}</code> placeholder will be replaced with the name of a dynamically generated JSONP callback function that AutoComplete will create. The server is expected to respond with a JavaScript value wrapped in a call to this callback function.
</p>

<p>
If the URL string does not include a <code>{callback}</code> placeholder, it will be called using XHR (XMLHttpRequest). XHR URLs <em>must</em> abide by the <a href="https://developer.mozilla.org/en/Same_origin_policy_for_JavaScript">same origin policy</a> or the browser will refuse to send the request. The server is expected to respond with valid JSON data, which AutoComplete will attempt to parse.
</p>

<pre class="code prettyprint">&#x2F;&#x2F; JSONP URL source.
Y.one(&#x27;#ac-input&#x27;).plug(Y.Plugin.AutoComplete, {
  source: &#x27;http:&#x2F;&#x2F;example.com&#x2F;search.jsonp?q={query}&amp;callback={callback}&#x27;
});

&#x2F;&#x2F; XHR URL source (no callback).
Y.one(&#x27;#ac-input&#x27;).plug(Y.Plugin.AutoComplete, {
  source: &#x27;http:&#x2F;&#x2F;example.com&#x2F;search.json?q={query}&#x27;
});</pre>


<p>
You may also optionally include a <code>{maxResults}</code> placeholder in the URL, which will be replaced with the value of the <code>maxResults</code> attribute (or 1000 if the <code>maxResults</code> attribute is less than or equal to 0).
</p>

<p>
Responses from JSONP and XHR URL sources are automatically cached based on the query value for the duration of the pageview on a per-instance basis (in other words, every AutoComplete instance has its own separate cache).
</p>

<h4 id="select-node"><code>&lt;select&gt;</code> Node</h4>

<p>
Set the <code>source</code> attribute to a <code>&lt;select&gt;</code> node to use its list of items as results. You'll also need to set the <code>resultTextLocator</code> attribute to 'text' or 'value' depending on what you want to use as the text of each result.
</p>

<pre class="code prettyprint">Y.one(&#x27;#ac-input&#x27;).plug(Y.Plugin.AutoComplete, {
    resultTextLocator: &#x27;text&#x27;,
    source: Y.one(&#x27;#my-list&#x27;)
});</pre>


<p>
Each result from a <code>&lt;select&gt;</code> source will be an object with the following properties:
</p>

<dl>
    <dt><code>html</code> <i>(String)</i></dt>
    <dd>
      <p>HTML content of the <code>&lt;option&gt;</code> element.</p>
    </dd>

    <dt><code>index</code> <i>(Number)</i></dt>
    <dd>
      <p>Index of the <code>&lt;option&gt;</code> element in the list.</p>
    </dd>

    <dt><code>node</code> <i>(Y.Node)</i></dt>
    <dd>
      <p>Node instance referring to the original <code>&lt;option&gt;</code> element.</p>
    </dd>

    <dt><code>selected</code> <i>(Boolean)</i></dt>
    <dd>
      <p>Whether or not this item is currently selected in the
      <code>&lt;select&gt;</code> list.</p>
    </dd>

    <dt><code>text</code> <i>(String)</i></dt>
    <dd>
      <p>Text content of the <code>&lt;option&gt;</code> element.</p>
    </dd>

    <dt><code>value</code> <i>(String)</i></dt>
    <dd>
      <p>Value of the <code>&lt;option&gt;</code> element.</p>
    </dd>
</dl>

<h4 id="yql-query">YQL Query</h4>

<p>
Set the <code>source</code> attribute to a <a href="http://developer.yahoo.com/yql/">YQL</a> query string to use that YQL query as the result source.
</p>

<p>
The string must include a <code>{query}</code> placeholder. On each query, AutoComplete will replace this with the current query and will make a call to YQL to get results.
</p>

<pre class="code prettyprint">Y.one(&#x27;#ac-input&#x27;).plug(Y.Plugin.AutoComplete, {
  source: &#x27;select * from search.suggest where query=&quot;{query}&quot;&#x27;
});</pre>


<p>
You may also optionally include a <code>{maxResults}</code> placeholder in the YQL query, which will be replaced with the value of the <code>maxResults</code> attribute (or 1000 if the <code>maxResults</code> attribute is less than or equal to 0).
</p>

<p>
AutoComplete does its best to automatically parse results out of YQL responses, but a wide variety of different YQL response formats are possible, so it may not always be possible for AutoComplete to guess the correct format. If you find that results aren't being parsed correctly, you may need to specify a custom <code>resultListLocator</code> and/or <code>resultTextLocator</code> as described in <a href="#locating-results">Locating Results</a>.
</p>

<h3 id="locating-results">Locating Results</h3>

<h4 id="result-list-locator">Result List Locator</h4>

<p>
It's not uncommon for a result source to return results inside a larger data structure, such as an object that contains other metadata about the response alongside the results.
</p>

<pre class="code prettyprint">{
  &quot;status&quot;: &quot;ok&quot;,
  &quot;query&quot;: &quot;sample response&quot;,

  &quot;data&quot;: {
    &quot;results&quot;: [
      &quot;foo&quot;,
      &quot;bar&quot;,
      &quot;baz&quot;
    ],

    &quot;resultCount&quot;: 3
  }
}</pre>


<p>
While AutoComplete automatically knows how to handle results that come back as a simple array, it needs some extra information in order to find a result array that's buried inside an object hierarchy. That's where the <code>resultListLocator</code> config attribute comes in.
</p>

<pre class="code prettyprint">Y.one(&#x27;#ac-input&#x27;).plug(Y.Plugin.AutoComplete, {
  resultListLocator: &#x27;data.results&#x27;,
  source: &#x27;http:&#x2F;&#x2F;example.com&#x2F;search.jsonp?q={query}&amp;callback={callback}&#x27;
});</pre>


<p>
In the example above, the <code>resultListLocator</code> tells AutoComplete to look for a <code>data</code> property on the response object, followed by a <code>results</code> sub-property that contains an array of results. The hierarchy may be arbitrarily deep, as long as it's consistent across responses.
</p>

<p>
If the response format isn't always the same, you can specify a function as the <code>resultListLocator</code> and run your own arbitrary logic to find (or construct) the result array. The function will receive the raw response as an argument, and must return an array of results.
</p>

<pre class="code prettyprint">&#x2F;&#x2F; Does the same thing as the previous example, but using a function.
Y.one(&#x27;#ac-input&#x27;).plug(Y.Plugin.AutoComplete, {
  resultListLocator: function (response) {
    return (response &amp;&amp; response.data &amp;&amp; response.data.results) || [];
  },

  source: &#x27;http:&#x2F;&#x2F;example.com&#x2F;search.jsonp?q={query}&amp;callback={callback}&#x27;
});</pre>


<h4 id="result-text-locator">Result Text Locator</h4>

<p>
Not all results are simple strings. Sometimes a result is an object containing lots of metadata, only part of which is a text string. In cases like this, AutoComplete needs to know how to find some text that it can display in the result list, use for highlighting, and insert into the input field when the user selects a result.
</p>

<p>
In the following sample, which is a subset of the data you might see in a response from the <a href="https://dev.twitter.com/docs/api/1/get/search">Twitter Search API</a>, an array of tweet objects is returned.
</p>

<pre class="code prettyprint">{
  &quot;query&quot;: &quot;documentation&quot;,
  &quot;results&quot;: [
    {
      &quot;from_user&quot;: &quot;yaypie&quot;,
      &quot;from_user_id&quot;: 3840589,
      &quot;from_user_id_str&quot;: &quot;3840589&quot;,
      &quot;created_at&quot;: &quot;Mon, 06 Dec 2010 22:58:08 +0000&quot;,
      &quot;id&quot;: 11917333878538241,
      &quot;id_str&quot;: &quot;11917333878538241&quot;,
      &quot;text&quot;: &quot;Is there such a thing as too much documentation?&quot;,
      &quot;profile_image_url&quot;: &quot;http:&#x2F;&#x2F;a3.twimg.com&#x2F;profile_images&#x2F;994441119&#x2F;ryan-profile-big_normal.jpg&quot;
    },

    ...
  ]
}</pre>


<p>
The <code>resultTextLocator</code> config attribute can be used to tell AutoComplete how to find some text within an individual result object, much like the <code>resultListLocator</code> attribute tells AutoComplete how to find an array of results within a response object.
</p>

<pre class="code prettyprint">Y.one(&#x27;#ac-input&#x27;).plug(Y.Plugin.AutoComplete, {
  resultListLocator: &#x27;results&#x27;,
  resultTextLocator: &#x27;text&#x27;,
  source: &#x27;http:&#x2F;&#x2F;search.twitter.com&#x2F;search.json?q={query}&amp;callback={callback}&#x27;
});</pre>


<p>
This tells AutoComplete that the value of each result object's <code>text</code> property should be used whenever a plain text form of the result is needed.
</p>

<p>
The <code>resultTextLocator</code> can also be a function, which allows you to perform additional logic, such as combining multiple values into a single text string. The function will receive a single result object as an argument, and must return a text string. It will be called once for each result in the results array.
</p>

<pre class="code prettyprint">Y.one(&#x27;#ac-input&#x27;).plug(Y.Plugin.AutoComplete, {
  resultListLocator: &#x27;results&#x27;,

  resultTextLocator: function (result) {
    return result.from_user + &#x27;: &#x27; + result.text;
  },

  source: &#x27;http:&#x2F;&#x2F;search.twitter.com&#x2F;search.json?q={query}&amp;callback={callback}&#x27;
});</pre>


<h3 id="filtering-results">Filtering Results</h3>

<p>
After results are retrieved from a result source, it may be necessary to perform additional filtering to whittle down the result list to match the query, especially if the result source doesn't perform filtering itself. The <code>resultFilters</code> attribute can be used to specify a filter or array of filters for this purpose.
</p>

<p>
A result filter is simply a function that accepts the current query and an array of result objects as arguments, and returns a filtered array of result objects.
</p>

<p>
The <code>autocomplete-filters</code> module provides a prepackaged collection of result filters. This module isn't loaded by default in the <code>autocomplete</code> rollup, but can be loaded manually as needed.
</p>

<pre class="code prettyprint">&#x2F;&#x2F; Include the autocomplete-filters module to use prepackaged filters.
YUI().use(&#x27;autocomplete&#x27;, &#x27;autocomplete-filters&#x27;, function (Y) {

  &#x2F;&#x2F; Specify the name of the prepackaged filter you want to use, as
  &#x2F;&#x2F; a string.
  Y.one(&#x27;#ac-input&#x27;).plug(Y.Plugin.AutoComplete, {
    resultFilters: &#x27;phraseMatch&#x27;
  });

  &#x2F;&#x2F; To use multiple filters, provide an array. Results will be passed
  &#x2F;&#x2F; through each filter in the order they&#x27;re listed in the array.
  Y.one(&#x27;#ac-input&#x27;).plug(Y.Plugin.AutoComplete, {
    resultFilters: [&#x27;charMatch&#x27;, &#x27;wordMatch&#x27;]
  });

});</pre>


<p>
The following filters are available in the <code>autocomplete-filters</code> module:
</p>

<table>
  <thead>
    <tr>
      <th>Filter</th>
      <th>Description</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><code>charMatch</code></td>
      <td>
        Returns results that contain all of the individual characters in the query, in any order (not necessarily consecutive).
      </td>
    </tr>

    <tr>
      <td><code>phraseMatch</code></td>
      <td>
        Returns results that contain the complete query as a phrase.
      </td>
    </tr>

    <tr>
      <td><code>startsWith</code></td>
      <td>
        Returns results that start with the complete query as a phrase.
      </td>
    </tr>

    <tr>
      <td><code>subWordMatch</code></td>
      <td>
        Returns results in which all the words of the query match either whole words or parts of words in the result. Non-word characters like whitespace and certain punctuation are ignored.
      </td>
    </tr>

    <tr>
      <td><code>wordMatch</code></td>
      <td>
        Returns results that contain all the individual words in the query, in any order (not necessarily consecutive).
      </td>
    </tr>
  </tbody>
</table>

<p>
By default, all filters are case-insensitive. Case-sensitive versions are available, and can be used by appending <code>Case</code> to the filter name. For example, the case-sensitive version of the <code>phraseMatch</code> filter is <code>phraseMatchCase</code>.
</p>

<p>
In addition to the standard set of filters, the optional <code>autocomplete-filters-accentfold</code> module provides a set of filters that perform accent-folded matching.
</p>

<p>
Accent folding is when a character like <code>é</code> is converted to a non-accented form like <code>e</code>. This can be useful for performing loose matching (such as matching the word "résumé" when the query "resume" is typed), but also has some important caveats you should be aware of. See the <a href="#known-issues">Known Issues</a> section below for details.
</p>

<p>
To use accent folding filters, include the <code>autocomplete-filters-accentfold</code> module, then specify a filter by appending <code>Fold</code> to the name. For example, the accent folding version of the <code>phraseMatch</code> filter is <code>phraseMatchFold</code>. Note that all accent folding filters are case-insensitive.
</p>

<h3 id="highlighting-results">Highlighting Results</h3>

<p>
After results are retrieved and (optionally) filtered, you may want to highlight occurrences of the query within each result in order to indicate to the user why that result is relevant to what they typed. The <code>resultHighlighter</code> attribute can be used to specify a result highlighter for this purpose.
</p>

<p>
Like a result filter, a highlighter is simply a function that accepts the current query and an array of result objects as arguments. Whereas filters return a filtered array of result objects, highlighters return an array of HTML strings which will be used when results are displayed to the user.
</p>

<p>
The <code>autocomplete-highlighters</code> module provides a prepackaged collection of result highlighters. This module isn't loaded by default in the <code>autocomplete</code> rollup, but can be loaded manually as needed.
</p>

<pre class="code prettyprint">&#x2F;&#x2F; Include the autocomplete-highlighters module to use prepackaged
&#x2F;&#x2F; highlighters.
YUI().use(&#x27;autocomplete&#x27;, &#x27;autocomplete-highlighters&#x27;, function (Y) {

  &#x2F;&#x2F; Specify the name of the prepackaged highlighter you want to
  &#x2F;&#x2F; use, as a string. Unlike result filters, only one highlighter
  &#x2F;&#x2F; may be used at a time.
  Y.one(&#x27;#ac-input&#x27;).plug(Y.Plugin.AutoComplete, {
    resultHighlighter: &#x27;phraseMatch&#x27;
  });

});</pre>


<p>
The following highlighters are available in the <code>autocomplete-highlighters</code> module:
</p>

<table>
  <thead>
    <tr>
      <th>Highlighter</th>
      <th>Description</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td><code>charMatch</code></td>
      <td>
        Highlights individual query characters that occur anywhere in the result, in any order (not necessarily consecutive).
      </td>
    </tr>

    <tr>
      <td><code>phraseMatch</code></td>
      <td>
        Highlights the complete query as a phrase anywhere in the result.
      </td>
    </tr>

    <tr>
      <td><code>startsWith</code></td>
      <td>
        Highlights the complete query as a phrase at the start of the result.
      </td>
    </tr>

    <tr>
      <td><code>subWordMatch</code></td>
      <td>
        Highlights results in which all the words of the query match either whole words or parts of words in the result. Non-word characters like whitespace and certain punctuation are ignored.
      </td>
    </tr>

    <tr>
      <td><code>wordMatch</code></td>
      <td>
        Highlights individual words in the result that are also in the query, in any order (not necessarily consecutive). Non-word characters like punctuation are ignored.
      </td>
    </tr>
  </tbody>
</table>

<p>
The prepackaged highlighters use a <code>&lt;b&gt;</code> element with the class <code>yui3-highlight</code> to highlight results. You can style the highlighting using CSS by referring to that class name.
</p>

<p>
By default, all highlighters are case-insensitive. Case-sensitive versions are available, and can be used by appending <code>Case</code> to the highlighter name. For example, the case-sensitive version of the <code>phraseMatch</code> highlighter is <code>phraseMatchCase</code>.
</p>

<p>
In addition to the standard set of highlighters, the optional <code>autocomplete-highlighters-accentfold</code> module provides a set of highlighters that perform accent-folded highlighting.
</p>

<p>
To use accent folding highlighters, include the <code>autocomplete-highlighters-accentfold</code> module, then specifiy a highlighter by appending <code>Fold</code> to the name. For example, the accent folding version of the <code>phraseMatch</code> highlighter is <code>phraseMatchFold</code>. Note that all accent folding highlighters are case-insensitive.
</p>

<p>
When using accent folding highlighters, there are some important caveats you should be aware of. See the <a href="#known-issues">Known Issues</a> section below for details.
</p>

<h2 id="customizing-autocomplete">Customizing AutoComplete</h2>
<h3 id="skinning">Skinning</h3>

<p>
AutoComplete uses the following CSS classes to provide skinning hooks for its markup. See the <a href="#dom-structure">DOM Structure</a> for an example of the markup generated by the AutoComplete widget.
</p>

<p>
You can add your own CSS to override the styling of these classes and customize the display and layout of the AutoComplete widget.
</p>

<table>
  <thead>
    <tr>
      <th>CSS Class</th>
      <th>Description</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td style="white-space:nowrap;"><code>yui3-aclist</code></td>
      <td>
        The <code>boundingBox</code> node that contains the rest of the AutoComplete list widget markup.
      </td>
    </tr>

    <tr>
      <td style="white-space:nowrap;"><code>yui3-aclist-aria</code></td>
      <td>
        ARIA live region container used to announce list updates to users of assistive tools. Positioned offscreen by default to make it invisible to sighted users.
      </td>
    </tr>

    <tr>
      <td style="white-space:nowrap;"><code>yui3-aclist-content</code></td>
      <td>
        The <code>contentBox</code> node that contains the AutoComplete list widget content. Apply visual styling like borders, padding, and margins to this node rather than the <code>boundingBox</code>.
      </td>
    </tr>

    <tr>
      <td style="white-space:nowrap;"><code>yui3-aclist-hidden</code></td>
      <td>
        Class added to elements that should be hidden from both sighted and unsighted users, such as when the AutoComplete list is hidden.
      </td>
    </tr>

    <tr>
      <td style="white-space:nowrap;"><code>yui3-aclist-input</code></td>
      <td>
        The <code>inputNode</code>. This node must already exist on the page and will not be created by AutoComplete, but AutoComplete will add the class name to make it consistently skinnable.
      </td>
    </tr>

    <tr>
      <td style="white-space:nowrap;"><code>yui3-aclist-item</code></td>
      <td>
        A single result item node inside the result list.
      </td>
    </tr>

    <tr>
      <td style="white-space:nowrap;"><code>yui3-aclist-item-active</code></td>
      <td>
        An active result item node.
      </td>
    </tr>

    <tr>
      <td style="white-space:nowrap;"><code>yui3-aclist-item-hover</code></td>
      <td>
        A result item node over which the mouse is currently hovering.
      </td>
    </tr>

    <tr>
      <td style="white-space:nowrap;"><code>yui3-aclist-list</code></td>
      <td>
        The <code>listNode</code> that contains result item nodes.
      </td>
    </tr>
  </tbody>
</table>

<h3 id="writing-result-filters">Writing Result Filters</h3>

<p>
As described in <a href="#filtering-results">Filtering Results</a>, a result filter is just a function that accepts the current query and an array of result objects as arguments, and returns a filtered array of result objects.
</p>

<p>
To use a custom filter, assign it to the <code>resultFilters</code> config attribute.
</p>

<pre class="code prettyprint">&#x2F;&#x2F; Simple example of a case-insensitive phrase matching custom
&#x2F;&#x2F; filter.
function customFilter(query, results) {
  query = query.toLowerCase();

  &#x2F;&#x2F; Iterate through the array of results and return a filtered
  &#x2F;&#x2F; array containing only results whose text includes the full
  &#x2F;&#x2F; query.
  return Y.Array.filter(results, function (result) {
    return result.text.toLowerCase().indexOf(query) !== -1;
  });
}

&#x2F;&#x2F; Create an AutoComplete instance that uses the custom filter.
Y.one(&#x27;#ac-input&#x27;).plug(Y.Plugin.AutoComplete, {
  resultFilters: customFilter
});</pre>


<h3 id="writing-result-highlighters">Writing Result Highlighters</h3>

<p>
A result highlighter is similar to a result filter, but instead of returning a filtered array of results, it returns an array of HTML strings.
</p>

<pre class="code prettyprint">&#x2F;&#x2F; Simple example of a case-insensitive custom phrase highlighter.
&#x2F;&#x2F; Uses Y.Highlight, which is provided by the &#x27;highlight&#x27; module.
function customHighlighter(query, results) {
  return Y.Array.map(results, function (result) {
    return Y.Highlight.all(result.text, query);
  });
}

&#x2F;&#x2F; Create an AutoComplete instance that uses the custom highlighter.
Y.one(&#x27;#ac-input&#x27;).plug(Y.Plugin.AutoComplete, {
  resultHighlighter: customHighlighter
});</pre>


<h3 id="writing-result-formatters">Writing Result Formatters</h3>

<p>
A result formatter is a function that receives the current query and an array of result objects as arguments, and must return an array of HTML strings or Node instances. Formatters run after filters and highlighters, and their output will be used for the display of result items in the final list of results.
</p>

<p>
An array of result objects created from a Twitter Search API response would look something like this after filtering and highlighting:
</p>

<pre class="code prettyprint">[
  {
    text: &#x27;Is there such a thing as too much documentation?&#x27;,
    highlighted: &#x27;Is there such a thing as too much &lt;b class=&quot;yui3-highlight&quot;&gt;documentation&lt;&#x2F;b&gt;?&#x27;,
    raw: {
      &quot;from_user&quot;: &quot;yaypie&quot;,
      &quot;from_user_id&quot;: 3840589,
      &quot;from_user_id_str&quot;: &quot;3840589&quot;,
      &quot;created_at&quot;: &quot;Mon, 06 Dec 2010 22:58:08 +0000&quot;,
      &quot;id&quot;: 11917333878538241,
      &quot;id_str&quot;: &quot;11917333878538241&quot;,
      &quot;text&quot;: &quot;Is there such a thing as too much documentation?&quot;,
      &quot;profile_image_url&quot;: &quot;http:&#x2F;&#x2F;a3.twimg.com&#x2F;profile_images&#x2F;994441119&#x2F;ryan-profile-big_normal.jpg&quot;
    }
  },

  ...
]</pre>


<p>
Using a result formatter, we can format these results as tweets in the result list instead of just displaying them as boring text.
</p>

<pre class="code prettyprint">&#x2F;&#x2F; HTML template string that will be used for each tweet result.
var tweetTemplate =
  &#x27;&lt;div class=&quot;tweet&quot;&gt;&#x27; +
    &#x27;&lt;div class=&quot;hd&quot;&gt;&#x27; +
      &#x27;&lt;img src=&quot;{profile_image_url}&quot; class=&quot;photo&quot; &#x27; +
        &#x27;alt=&quot;Profile photo for {from_user}&quot;&gt;&#x27; +
    &#x27;&lt;&#x2F;div&gt;&#x27; +
    &#x27;&lt;div class=&quot;bd&quot;&gt;&#x27; +
      &#x27;&lt;strong class=&quot;user&quot;&gt;{from_user}&lt;&#x2F;strong&gt;&#x27; +
      &#x27;&lt;span class=&quot;tweet-text&quot;&gt;{highlighted}&lt;&#x2F;span&gt;&#x27; +
    &#x27;&lt;&#x2F;div&gt;&#x27; +
    &#x27;&lt;div class=&quot;ft&quot;&gt;{created_at}&lt;&#x2F;div&gt;&#x27; +
  &#x27;&lt;&#x2F;div&gt;&#x27;;

&#x2F;&#x2F; Custom formatter for tweets.
function tweetFormatter(query, results) {
  &#x2F;&#x2F; Iterate over the array of tweet result objects and return an
  &#x2F;&#x2F; array of HTML strings.
  return Y.Array.map(results, function (result) {
    var tweet = result.raw;

    &#x2F;&#x2F; Use string substitution to fill out the tweet template and
    &#x2F;&#x2F; return an HTML string for this result.
    return Y.Lang.sub(tweetTemplate, {
      created_at       : tweet.created_at,
      from_user        : tweet.from_user,
      highlighted      : result.highlighted,
      profile_image_url: tweet.profile_image_url
    });
  });
}

&#x2F;&#x2F; Instantiate AutoComplete using the custom formatter.
Y.one(&#x27;#ac-input&#x27;).plug(Y.Plugin.AutoComplete, {
  resultFormatter: tweetFormatter,
  resultHighlighter: &#x27;phraseMatch&#x27;,
  resultListLocator: &#x27;results&#x27;,
  resultTextLocator: &#x27;text&#x27;,
  source: &#x27;http:&#x2F;&#x2F;search.twitter.com&#x2F;search.json?q={query}&amp;callback={callback}&#x27;
});</pre>


<p>
Add some CSS to make things pretty, and you're good to go.
</p>

<h2 id="extending-autocompletebase">Extending <code>AutoCompleteBase</code></h2>

<p>
The <code>Y.AutoCompleteBase</code> class provides the core logic for a generic implementation of the autocomplete pattern, but without any UI-related functionality or implementation code. It's meant to be used as a <code>Y.Base</code> or <code>Y.Widget</code> extension and mixed into another class that builds an implementation on top of it (this is what <code>Y.AutoCompleteList</code> does).
</p>

<p>
You can take advantage of <code>AutoCompleteBase</code> to build a customized implementation of the autocomplete pattern that doesn't necessarily have to use a traditional list-based suggestion UI.
</p>

<h3 id="as-a-standalone-class">As a Standalone Class</h3>

<p>
The following skeleton demonstrates how to create a standalone class that mixes in the <code>AutoCompleteBase</code> extension, making it instantiable and usable as an API, but without any user-visible interface.
</p>

<pre class="code prettyprint">&#x2F;&#x2F; Create a custom class that mixes in the AutoCompleteBase extension.
var MyAutoComplete = Y.Base.create(&#x27;myAC&#x27;, Y.Base, [Y.AutoCompleteBase], {
  initializer: function () {
    &#x2F;&#x2F; The following two function calls allow AutoComplete to attach
    &#x2F;&#x2F; events to the inputNode and manage the inputNode&#x27;s browser
    &#x2F;&#x2F; autocomplete functionality.
    this._bindUIACBase();
    this._syncUIACBase();
  }

  &#x2F;&#x2F; Custom prototype methods and properties here (optional).

}, {

  &#x2F;&#x2F; Custom static methods and properties here (optional).

});

&#x2F;&#x2F; Create a new instance of the custom MyAutoComplete class, with
&#x2F;&#x2F; an array result source.
var ac = new MyAutoComplete({
  inputNode: &#x27;#ac-input&#x27;,
  source: [&#x27;foo&#x27;, &#x27;bar&#x27;, &#x27;baz&#x27;]
});</pre>


<p>
From an interaction standpoint, this is a little like using AutoComplete as a blank slate. It will allow you to attach AutoComplete to an input node and hook into AutoComplete's API and events so you get result retrieval, filtering, highlighting, formatting, etc., but anything beyond that is up to you.
</p>

<h3 id="as-a-widget-class">As a Widget Class</h3>

<p>
Building your own widget on top of <code>AutoCompleteBase</code> is easy. Just mix <code>AutoCompleteBase</code> into your widget class, then flesh it out with your own custom widget implementation. Your widget will inherit all of <code>AutoCompleteBase</code>'s methods, attributes, and events.
</p>

<pre class="code prettyprint">&#x2F;&#x2F; Create a custom widget that mixes in the AutoCompleteBase extension.
var MyAutoComplete = Y.Base.create(&#x27;myAC&#x27;, Y.Widget, [Y.AutoCompleteBase], {

  &#x2F;&#x2F; Custom prototype methods and properties here (optional).

}, {

  &#x2F;&#x2F; Custom static methods and properties here (optional).

});

&#x2F;&#x2F; Create a new instance of the custom MyAutoComplete widget, with
&#x2F;&#x2F; an array result source.
var ac = new MyAutoComplete({
  inputNode: &#x27;#ac-input&#x27;,
  source: [&#x27;foo&#x27;, &#x27;bar&#x27;, &#x27;baz&#x27;]
});</pre>


<h2 id="accessibility">Accessibility</h2>

<p>
AutoComplete is designed to be accessible out of the box to users of screen readers and other assistive tools. This is accomplished via a combination of progressive enhancement and adherence to <a href="http://www.w3.org/WAI/intro/aria">WAI-ARIA</a> best practices, which help to convey the meaning and behavior of the AutoComplete widget to users who may not be able to see or interact with it in a conventional way.
</p>

<h3 id="keyboard-interaction">Keyboard Interaction</h3>

<p>
The AutoComplete widget supports the following keyboard commands.
</p>

<table>
  <thead>
    <tr>
      <th>Key</th>
      <th>Description</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td style="white-space: nowrap;">Down arrow</td>
      <td>
        Activates the next item in the list, or displays the list if the list is currently hidden. Wraps around to the top of the list if the <code>circular</code> config attribute is set to <code>true</code> and there is no next item.
      </td>
    </tr>

    <tr>
      <td style="white-space: nowrap;">Enter</td>
      <td>
        When the list is visible and an item is active, selects the currently active item and hides the list.
      </td>
    </tr>

    <tr>
      <td style="white-space: nowrap;">Escape</td>
      <td>
        Hides the list if it's currently visible.
      </td>
    </tr>

    <tr>
      <td style="white-space: nowrap;">Tab</td>
      <td>
        When the list is visible and an item is active, selects the currently active item and hides the list.
      </td>
    </tr>

    <tr>
      <td style="white-space: nowrap;">Up arrow</td>
      <td>
        Activates the previous item in the list. Wraps around to the bottom of the list if the <code>circular</code> config attribute is set to <code>true</code> and there is no previous item.
      </td>
    </tr>
  </tbody>
</table>

<p>
Keyboard functionality is not loaded by default for users of iOS and Android-based devices, since these devices typically don't provide support for keyboard interaction. If for some reason you want to load the keyboard code for these devices, include the <code>autocomplete-list-keys</code> module in your <code>YUI().use()</code> statement.
</p>

<h3 id="aria">ARIA</h3>

<p>
When the AutoComplete widget is rendered, it adds the following ARIA attributes to the <code>inputNode</code>:
</p>

<dl>
  <dt><code>aria-activedescendant="<i>activeItem id</i>"</code></dt>
  <dd>
    Indicates that the specified result item node is the active (pre-selected) item in the list owned by this <code>inputNode</code>. This attribute is added or removed automatically when the <code>activeItem</code> changes.
  </dd>

  <dt><code>aria-autocomplete=&quot;list&quot;</code></dt>
  <dd>
    Indicates that the <code>inputNode</code> provides autocomplete suggestions in the form of a list as the user types.
  </dd>

  <dt><code>aria-expanded="<i>true|false</i>"</code></dt>
  <dd>
    Indicates whether the result list is expanded (true) or collapsed (false). This attribute will be updated automatically when the list's state changes.
  </dd>

  <dt><code>aria-owns="<i>listNode id</i>"</code></dt>
  <dd>
    <p>
    Indicates that there is a parent/child relationship between the <code>inputNode</code> (which accepts user input) and the <code>listNode</code> (which displays autocomplete results related to that input).
    </p>

    <p>
    Not all assistive tools support the <code>aria-owns</code> attribute. For this reason, it's strongly recommended that you allow the AutoComplete widget to render its list markup inside the same container element as the input node, and immediately after the input node (this is the default behavior) rather than specifying a different parent node.
    </p>
  </dd>
</dl>

<p>
The list markup, which is rendered dynamically, uses the following ARIA attributes:
</p>

<dl>
  <dt><code>aria-hidden="<i>true|false</i>"</code></dt>
  <dd>
    Applied to the <code>boundingBox</code>. Indicates whether the result list is hidden (true) or visible (false). This attribute will be updated automatically when the lists's state changes.
  </dd>

  <dt><code>role=&quot;listbox&quot;</code></dt>
  <dd>
    Applied to the <code>listNode</code>. Indicates that this node represents a widget that allows the user to select one or more items from a list of choices.
  </dd>

  <dt><code>role=&quot;option&quot;</code></dt>
  <dd>
    Applied to individual result item nodes inside the <code>listNode</code>. Indicates that the node represents a selectable item in a list.
  </dd>
</dl>

<h2 id="known-issues">Known Issues</h2>

<ul>
  <li>
    <p>
    The accent folding and word breaking implementations require the use of UTF-8 character data. If YUI is used on a page that doesn't specify a UTF-8 charset either via the <code>Content-Type</code> HTTP response header or a <code>&lt;meta charset=&quot;utf-8&quot;&gt;</code> tag, an "Invalid range in character set" error may occur in Internet Explorer when the page is loaded from the cache. Specify a charset to avoid triggering this IE bug (you should always specify a charset in any case, since all kinds of bad things can happen if you don't).
    </p>
  </li>

  <li>
    <p>
    The accent folding implementation used for filters and highlighters is not comprehensive, since it wouldn't be practical to serve a complete set of character data to clients via JS. The implementation used here provides basic accent folding for common alphanumeric characters only, and is not locale-aware. Whenever possible, accent folding should be done on the server, where more complete character data can be used, and not on the client.
    </p>
  </li>

  <li>
    <p>
    When used for matching, accent folding is likely to produce erroneous matches for languages in which characters with diacritics are considered different from their base characters, or where correct folding would map to other character sequences than just stripped characters.
    </p>

    <p>
    For example, in German "ü" is a character that's clearly different from "u" and should match "ue" instead. The word "betrügen" means "to defraud", while "betrugen" is the past tense of "to behave". The name "Müller" is expected to match "Mueller", but not "Muller".
    </p>

    <p>
    On the other hand, accent folding falls short for languages where different base characters are expected to match. In Japanese, for example, hiragana and katakana characters with the same pronunciation ("あ" and "ア") are commonly treated as equivalent for lookups, but accent folding treats them as different.
    </p>
  </li>

  <li>
    <p>
    The result highlighters provided by the <code>autocomplete-highlighters</code> module may introduce unwanted word breaks in Arabic, Syriac, and N'Ko scripts. This issue is being tracked in <a href="http://yuilibrary.com/projects/yui3/ticket/2529396">ticket #2529396</a>, and will be fixed in a future release.
    </p>
  </li>
</ul>
</div>
        </div>

        <div id="sidebar" class="yui3-u">
            
                <div id="toc" class="sidebox">
                    <div class="hd">
                        <h2 class="no-toc">Table of Contents</h2>
                    </div>

                    <div class="bd">
                        <ul class="toc">
<li>
<a href="#getting-started">Getting Started</a>
</li>
<li>
<a href="#using-autocomplete">Using AutoComplete</a>
<ul class="toc">
<li>
<a href="#quick-start">Quick Start</a>
</li>
<li>
<a href="#instantiating-autocomplete">Instantiating AutoComplete</a>
<ul class="toc">
<li>
<a href="#as-a-plugin">As a Plugin</a>
</li>
<li>
<a href="#as-a-class">As a Class</a>
</li>
</ul>
</li>
<li>
<a href="#dom-structure">DOM Structure</a>
</li>
<li>
<a href="#configuring-autocomplete">Configuring AutoComplete</a>
<ul class="toc">
<li>
<a href="#base-config-attributes">Base Config Attributes</a>
</li>
<li>
<a href="#list-config-attributes">List Config Attributes</a>
</li>
<li>
<a href="#aligning-the-list">Aligning the List</a>
</li>
<li>
<a href="#using-query-delimiters">Using Query Delimiters</a>
</li>
</ul>
</li>
<li>
<a href="#autocomplete-events">AutoComplete Events</a>
<ul class="toc">
<li>
<a href="#base-events">Base Events</a>
</li>
<li>
<a href="#list-events">List Events</a>
</li>
</ul>
</li>
<li>
<a href="#result-sources">Result Sources</a>
<ul class="toc">
<li>
<a href="#arrayobject">Array/Object</a>
</li>
<li>
<a href="#datasource">DataSource</a>
</li>
<li>
<a href="#function">Function</a>
</li>
<li>
<a href="#jsonpxhr-url">JSONP/XHR URL</a>
</li>
<li>
<a href="#select-node"><code>&lt;select&gt;</code> Node</a>
</li>
<li>
<a href="#yql-query">YQL Query</a>
</li>
</ul>
</li>
<li>
<a href="#locating-results">Locating Results</a>
<ul class="toc">
<li>
<a href="#result-list-locator">Result List Locator</a>
</li>
<li>
<a href="#result-text-locator">Result Text Locator</a>
</li>
</ul>
</li>
<li>
<a href="#filtering-results">Filtering Results</a>
</li>
<li>
<a href="#highlighting-results">Highlighting Results</a>
</li>
</ul>
</li>
<li>
<a href="#customizing-autocomplete">Customizing AutoComplete</a>
<ul class="toc">
<li>
<a href="#skinning">Skinning</a>
</li>
<li>
<a href="#writing-result-filters">Writing Result Filters</a>
</li>
<li>
<a href="#writing-result-highlighters">Writing Result Highlighters</a>
</li>
<li>
<a href="#writing-result-formatters">Writing Result Formatters</a>
</li>
</ul>
</li>
<li>
<a href="#extending-autocompletebase">Extending <code>AutoCompleteBase</code></a>
<ul class="toc">
<li>
<a href="#as-a-standalone-class">As a Standalone Class</a>
</li>
<li>
<a href="#as-a-widget-class">As a Widget Class</a>
</li>
</ul>
</li>
<li>
<a href="#accessibility">Accessibility</a>
<ul class="toc">
<li>
<a href="#keyboard-interaction">Keyboard Interaction</a>
</li>
<li>
<a href="#aria">ARIA</a>
</li>
</ul>
</li>
<li>
<a href="#known-issues">Known Issues</a>
</li>
</ul>
                    </div>
                </div>
            

            
                <div class="sidebox">
                    <div class="hd">
                        <h2 class="no-toc">Examples</h2>
                    </div>

                    <div class="bd">
                        <ul class="examples">
                            
                                
                                    <li data-description="How to provide autocomplete suggestions from a local array.">
                                        <a href="ac-local.html">Basic Local Data</a>
                                    </li>
                                
                            
                                
                                    <li data-description="How to provide autocomplete suggestions using a remote JSONP source.">
                                        <a href="ac-jsonp.html">Remote Data via JSONP</a>
                                    </li>
                                
                            
                                
                                    <li data-description="How to provide autocomplete suggestions using a YQL query source.">
                                        <a href="ac-yql.html">Remote Data via YQL</a>
                                    </li>
                                
                            
                                
                                    <li data-description="How to provide autocomplete suggestions using a DataSource instance.">
                                        <a href="ac-datasource.html">Remote Data via DataSource</a>
                                    </li>
                                
                            
                                
                                    <li data-description="How to implement delimited tag completion.">
                                        <a href="ac-tagging.html">Tag Completion Using Query Delimiters</a>
                                    </li>
                                
                            
                                
                                    <li data-description="How to find and select Flickr photos using a YQL source and a custom autocomplete result formatter.">
                                        <a href="ac-flickr.html">Find Photos on Flickr (Custom Formatting, YQL Source)</a>
                                    </li>
                                
                            
                                
                                    <li data-description="How to use autocomplete-base to filter a set of existing items on a page.">
                                        <a href="ac-filter.html">Filter a Set of Existing Items on a Page</a>
                                    </li>
                                
                            
                        </ul>
                    </div>
                </div>
            

            
        </div>
    </div>
</div>

<script src="../assets/vendor/prettify/prettify-min.js"></script>
<script>prettyPrint();</script>

</body>
</html>
